package chixing.day16.class23;

import java.io.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 面向对象，集合，数据类型转换，IO
 */
public class BufferedWriterDemo {
    private   static  List<Record> recordList = new ArrayList<>();
    private   static  List<Record> exceptionRecordList = new ArrayList<>();
    public static void main(String[] args) {
        readFlowFile("d://flow.txt");
        System.out.println("-------有效的交易记录-----------");
        recordList.forEach(System.out::println);

        System.out.println("-------无效的交易记录-----------");
        exceptionRecordList.forEach(System.out::println);
    }


    public static void readFlowFile(String filePath){

        // 带有缓冲的读取流
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader( new InputStreamReader( new FileInputStream(filePath)));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }

        String recordStr = null;
        try {
            String title = bufferedReader.readLine();  // 第一行是标题行,读取了， 不拿
            while((recordStr = bufferedReader.readLine())!=null){
                Record record = parseToRecord(recordStr);
                if(record.getTransactionDate() == null)   // 交易日期为null , 添加到异常的集合中
                    exceptionRecordList.add(record);
                else
                    recordList.add(record);
            }
        }catch (IOException e){
            e.printStackTrace();
        } finally {
            if(bufferedReader !=null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }

    //    000001|张晓敏|0000|1|4155990188888888|20060720200005|300.00
    // 将字符串流水记录 转换成 Record对象
    public static Record parseToRecord(String line){
        Record record = null;
        String[] split = line.split("\\|");

        String transactionNumber = split[0];

        String userName = split[1];
        String institutionNumber = split[2];
        String userGenderStr = split[3];  // char
        char userGender = userGenderStr.equals("1")?'F':'M';
        String flowingWaterNumber = split[4];
        String transactionAmountStr = split[6]; // "300.00"
        double transactionAmount = Double.valueOf(transactionAmountStr);

        String transactionDateStr = split[5];  // "null"
        LocalDateTime transactionDate = null;
        if(!"null".equals(transactionDateStr)){
            // 正常交易日期数据，正常处理
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
            transactionDate = LocalDateTime.parse(transactionDateStr, fmt);
        }
        record =  new Record(transactionNumber, userName, institutionNumber, userGender,
                flowingWaterNumber, transactionDate, transactionAmount);

        return record;
    }
}

